[% setvar title Variable interpolation on demand. %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Variable interpolation on demand.</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Glenn Linderman &lt;<a href='mailto:glenn@linderman.com'>glenn@linderman.com</a>&gt;
  Date: 14 Sep 2000
  Last Modified: 28 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 229
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Make Perl's powerful string interpolation facilities are available to
variables, in addition to literals.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<p>Version 2 adds more description to the IMPLEMENTATION section.</p>
<p>Notes on  Freeze: not  much discussion resulted  on the  list.  Someone
suggested  the  implementation  now  mentioned  in  the  implementation
section, but it isn't really general enough due to the need to choose a
delimiter character,  although it  works for some  strings some  of the
time.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Given:</p>
<p>$foo = 'def';
$bar = 'ghi';
$x = &quot;abc$foo$bar&quot;;
$y = 'abc$foo$bar';</p>
<p>There is no  way to turn obtain the  value of $x from the  value of $y.
In other  words, while  $foo and $bar  were interpolated into  $x, they
were not interpolated into $y.  It would be nice to get Success! from:</p>
<p>$z = interpolate ( $y );
print 'Success!&quot;  if $z eq $x;</p>
<p>However,  there  is  no  direct  language facility  for  this,  because
interpolation only happens one level  deep (which is good), so attempts
such as</p>
<p>$z = qq/$y/;</p>
<p>will interpolate $y, but not $foo and  $bar, so $z eq $y, instead of $z
eq $x.</p>
<p>Shown above is a functional  syntax.  However, due to scoping rules and
lexical  variables, it  is unlikely  that this  functionality  could be
implemented as a  function, more likely it would have  to be a keyword.
An alternate syntax might be:</p>
<p>$z = qd/$y/;</p>
<p>implying a double interpolation pass.  Or</p>
<p>$z = q2/$y/;</p>
<p>which means 2 interpolation passes,  and which could be extended to q3,
q4, q5 ???  Or</p>
<p>$z = qq/$y/2;</p>
<p>which likewise would mean two passes, and could be extended to 3, 4, 5,
or even to</p>
<p>$z = qq/$y/$n;</p>
<p>a  variable number  of passes.   The same  could be  achieved  with the
keyword technique via an additional parameter:</p>
<p>interpolate ( $y, $n );</p>
<a name='Compatibility considerations'></a><h2>Compatibility considerations</h2>
<p>This is a new feature, so name conflict is the only issue.</p>
<p>This    seems   compatible    with   other    extensions    to   string
interpolation... whatever  extensions get implemented  should work here
too.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Just a re-write rule.</p>
<p>interpolate ( $y )</p>
<p>could become</p>
<p>eval &quot;qq\000&quot;.$y.&quot;\0&quot;;</p>
<p>if  you make  the assumption  that  null characters  would never  exist
within $y.  Of course, that is an invalid assumption in Perl.  Avoiding
that assumption  would either require that  you scan $y  and discover a
character not contained  within it that can be  used for the delimiter,
or  instead  of the  eval,  make a  direct  call  to the  interpolation
function inside Perl.  This latter is probably the better solution, and
explains why this should be in core.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>None.</p>
</div>
